package org.springblade.common.tool;

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

public class UploadFileTest {

	public static String upload(String filePath, String url, String dis, String pixle,
								String xiangjiao, String f) throws Exception {
		CloseableHttpClient httpclient = HttpClients.createDefault();
		try {
			HttpPost httppost = new HttpPost(url);
//			FileBody bin = new FileBody(new File(filePath));
			StringBody comment = new StringBody("A binary file of some kind", ContentType.TEXT_PLAIN);
			HttpEntity reqEntity = MultipartEntityBuilder.create()
				.addTextBody("path", filePath)
				.addPart("comment", comment)
				.addTextBody("dis", dis)
				.addTextBody("pixle", pixle)
				.addTextBody("xiangjiao", xiangjiao)
				.addTextBody("f", f)
				.build();


			httppost.setEntity(reqEntity);

			System.out.println("executing request " + httppost.getRequestLine());
			CloseableHttpResponse response = httpclient.execute(httppost);
			try {
				HttpEntity resEntity = response.getEntity();
				HttpEntity entity = response.getEntity();
				if (entity != null) {
					String result = EntityUtils.toString(entity);
					response.close();
					return result;
				}
				if (resEntity != null) {
					System.out.println("Response content length: " + resEntity.getContentLength());
				}
				EntityUtils.consume(resEntity);
			} finally {
				response.close();
			}
		} finally {
			httpclient.close();
		}
		return "0";
	}

	public static void main(String[] args) {
		try {
			//	System.out.println(upload("D:\\images\\upload\\image\\20190415\\1.jpg", "http://192.168.10.165:8086/build"));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
